# -*- coding:utf-8 -*-

import matplotlib.pyplot as plt
import re
from scipy.misc import imread
from wordcloud import WordCloud, STOPWORDS,ImageColorGenerator


def ciyun(log, exp_pic):
    tmp = open(log).read()
    tmp_list = re.findall(r'<.*?>', tmp, re.S)
    for i in tmp_list:
        tmp = tmp.replace(i, '')
    text = tmp.replace('log url', '')

    alice_coloring = imread(exp_pic)
    wc = WordCloud(background_color="white",
                   mask=alice_coloring,
                   stopwords=STOPWORDS.add('Owner'),
                   max_font_size=80,
                   random_state=50,
                   max_words=1000
                   )

    wc.generate(text)
    ImageColorGenerator(alice_coloring)

    plt.imshow(wc)
    plt.axis("off")
    #plt.show()

    pic_path = '/'.join(log.split('/')[:-1])+'/new_pic.png'
    # print pic_path, log
    wc.to_file(pic_path)
    return pic_path


if __name__ == "__main__":
    ciyun("/var/lib/libvirt/log/xbu/et_test/not_analyzed_2018-02-22_10-07", '/home/xbu/git/co_work/ET_testlog/ET.png')